package com.base.core.context.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;

import com.gitee.magic.core.exception.ApplicationException;
import com.gitee.magic.framework.base.constant.Config;
import com.gitee.magic.framework.head.utils.TimeUtils;

/**
 * 许可证
 * @author zywei
 *
 */
@Component
public class LicenseDetectionProcessor {
	
	@Value("${license.detection.day:7}")
	private Integer day;

	@Autowired
	private ConfigurableEnvironment environment;
	
	private Date lastTime=new Date();
	
	public synchronized void check() {
		if(!Config.isSystemFlag()) {
			return;
		}
		if(lastTime.after(new Date())) {
			return;
		}
		try {
			GiteeConfigureEnvironmentPostProcessor gcep=new GiteeConfigureEnvironmentPostProcessor();
			gcep.postProcessEnvironment(environment, null);
			lastTime=TimeUtils.getDate(day);
		}catch(Exception e) {
			throw new ApplicationException(e);
		}
	}
	
}
